// const baseUrl = 'https://slem.a.hlidc.cn/api/';
const baseUrl = 'https://www.slem.com.au/api/';
// const baseUrl = 'https://apps.slem.com.au/api/';
export default function request(url, data = {}, method = 'GET', isLoading = true) {
	let token = uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		if (isLoading) {
			uni.showLoading({
				title: 'loading',
				mask: true,
			});
		}
		const currentLang = uni.getStorageSync('lang') ? uni.getStorageSync('lang') : 'zh-cn'
		let headers = {
			'Content-Type': 'application/json'
		}
		if (token) {
			headers.token = token
		}
		uni.request({
			url: baseUrl + url + `?lang=${currentLang}`,
			method: method,
			data,
			header: headers,
			success: (res) => {
				if (isLoading) {
					uni.hideLoading();
				}
				const {
					data,
					code,
					msg
				} = res.data
				// console.log(data, code, `----${url}的result----`)
				if (code == 0) {
					uni.showToast({
						duration: 1000,
						title: msg,
						icon: 'none'
					})
					return
				}
				if (code == 1) {
					resolve(data)
				}
				if (code == 401) {
					uni.showToast({
						title: currentLang == "zh-cn" ? "����¼������" : "Please login",
						icon: 'error'
					})
					setTimeout(() => {
						uni.removeStorageSync('token')
						uni.reLaunch({
							url: '/pages/tabBar/login'
						})
					}, 1000)


				}
			},
			fail: (err) => {
				wx.showToast({
					title: 'error',
					icon: 'error'
				})
				uni.hideLoading();
			},
		});
	});
}